/*
 * DungeonResource.cpp
 *
 *  Created on: 2013. 9. 4.
 *      Author: mega@gamevil.com
 */

#include "../User.h"
#include "DungeonResource.h"
#include "../NUtil/Utils.h"
#include "../engine.h"
#include "../macro.h"

namespace ProjectZ { namespace NResource {

DungeonResource::DungeonResource()
{
	pDungeonAndroid	= new DungeonData;
	pDungeonIOS		= new DungeonData;
}

DungeonResource::~DungeonResource()
{
	delete pDungeonAndroid;
	delete pDungeonIOS;
}

bool DungeonResource::LoadResource()
{
	if (false == pDungeonAndroid->LoadResource(MARKET_GOOGLE, SC_KAKAO_GOOGLE))
		return false;

	if (false == pDungeonIOS->LoadResource(MARKET_IOS, SC_KAKAO_IOS))
		return false;

	return true;
}

DungeonData::Flyweight * DungeonResource::GetFly(int company, int saleCode, int dungeonid)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(company, saleCode);

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pDungeonAndroid->GetFly(dungeonid);

	case NUtil::MarketType::IOS_KAKAO:
		return pDungeonIOS->GetFly(dungeonid);

	default:
		return NULL;
	}

	return NULL;
}


DungeonData::~DungeonData()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
}

void
DungeonData::Clear()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
	clsFlyTree.clear();
}

bool DungeonData::LoadResource(int company, int saleCode)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT * FROM resource_dungeon WHERE company=%d AND sale_cd=%d", company, saleCode);

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	Clear();

	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		int tmpCompany, tmpSalecd;

		DungeonData::Flyweight * pFly = new DungeonData::Flyweight;

		clsAuto->GetRecord() >> NATIONAL_CODE >> tmpCompany >> tmpSalecd
				>> pFly->THEME >> pFly->DUNGEON >> pFly->OPEN_LV
				>> pFly->SIZE >> pFly->LENGTH >> pFly->STAMINA >> pFly->MOB1_TID
				>> pFly->MOB1_LV_NORMAL_NORMAL >> pFly->MOB1_LV_HARD
				>> pFly->MOB1_LV_HELL >> pFly->MOB1_LIMIT >> pFly->DROP1_ITEM1
				>> pFly->DROP1_ITEM2 >> pFly->DROP1_ITEM3
				>> pFly->DROP1_ITEM4_TYPE >> pFly->DROP1_ITEM4
				>> pFly->QUEST1_IDX >> pFly->QUEST1_ITEM >> pFly->EVENT1_ITEM_TYPE
				>> pFly->EVENT1_ITEM >> pFly->MOB2_TID >> pFly->MOB2_LV_NORMAL
				>> pFly->MOB2_LV_HARD >> pFly->MOB2_LV_HELL >> pFly->MOB2_LIMIT
				>> pFly->DROP2_ITEM1 >> pFly->DROP2_ITEM2 >> pFly->DROP2_ITEM3
				>> pFly->DROP2_ITEM4_TYPE >> pFly->DROP2_ITEM4
				>> pFly->QUEST2_IDX >> pFly->QUEST2_ITEM >> pFly->EVENT2_ITEM_TYPE
				>> pFly->EVENT2_ITEM >> pFly->MOB3_TID >> pFly->MOB3_LV_NORMAL
				>> pFly->MOB3_LV_HARD >> pFly->MOB3_LV_HELL >> pFly->MOB3_LIMIT
				>> pFly->DROP3_ITEM1 >> pFly->DROP3_ITEM2 >> pFly->DROP3_ITEM3
				>> pFly->DROP3_ITEM4_TYPE >> pFly->DROP3_ITEM4
				>> pFly->QUEST3_IDX >> pFly->QUEST3_ITEM >> pFly->EVENT3_ITEM_TYPE
				>> pFly->EVENT3_ITEM >> pFly->MOB4_TID >> pFly->MOB4_LV_NORMAL
				>> pFly->MOB4_LV_HARD >> pFly->MOB4_LV_HELL >> pFly->MOB4_LIMIT
				>> pFly->DROP4_ITEM1 >> pFly->DROP4_ITEM2 >> pFly->DROP4_ITEM3
				>> pFly->DROP4_ITEM4_TYPE >> pFly->DROP4_ITEM4
				>> pFly->QUEST4_IDX >> pFly->QUEST4_ITEM >> pFly->EVENT4_ITEM_TYPE
				>> pFly->EVENT4_ITEM >> pFly->MOB5_TID >> pFly->MOB5_LV_NORMAL
				>> pFly->MOB5_LV_HARD >> pFly->MOB5_LV_HELL >> pFly->MOB5_LIMIT
				>> pFly->DROP5_ITEM1 >> pFly->DROP5_ITEM2 >> pFly->DROP5_ITEM3
				>> pFly->DROP5_ITEM4_TYPE >> pFly->DROP5_ITEM4
				>> pFly->QUEST5_IDX >> pFly->QUEST5_ITEM >> pFly->EVENT5_ITEM_TYPE
				>> pFly->EVENT5_ITEM >> pFly->MOB6_TID >> pFly->MOB6_LV_NORMAL
				>> pFly->MOB6_LV_HARD >> pFly->MOB6_LV_HELL >> pFly->MOB6_LIMIT
				>> pFly->DROP6_ITEM1 >> pFly->DROP6_ITEM2 >> pFly->DROP6_ITEM3
				>> pFly->DROP6_ITEM4_TYPE >> pFly->DROP6_ITEM4
				>> pFly->QUEST6_IDX >> pFly->QUEST6_ITEM >> pFly->EVENT6_ITEM_TYPE
				>> pFly->EVENT6_ITEM >> pFly->LEGEND_TID
				>> pFly->LEGEND_LV_NORMAL >> pFly->LEGEND_LV_HARD
				>> pFly->LEGEND_LV_HELL >> pFly->LEGEND_APPEAR_RATE
				>> pFly->DROP_LEGEND_ITEM1 >> pFly->DROP_LEGEND_ITEM2
				>> pFly->DROP_LEGEND_ITEM3 >> pFly->DROP_LEGEND_ITEM4_TYPE
				>> pFly->DROP_LEGEND_ITEM4 >> pFly->QUEST_LEGEND_IDX >> pFly->QUEST_LEGEND_ITEM
				>> pFly->EVENT_LEGEND_ITEM >> pFly->BOSS_MAP >> pFly->BOSS_TID
				>> pFly->BOSS_LV_NORMAL >> pFly->BOSS_LV_HARD
				>> pFly->BOSS_LV_HELL >> pFly->DROP_BOSS_ITEM1
				>> pFly->DROP_BOSS_ITEM2 >> pFly->DROP_BOSS_ITEM3
				>> pFly->DROP_BOSS_ITEM4_TYPE >> pFly->DROP_BOSS_ITEM4
				>> pFly->DAY_ITEM_0 >> pFly->DAY_ITEM_1
				>> pFly->DAY_ITEM_2 >> pFly->DAY_ITEM_3 >> pFly->DAY_ITEM_4
				>> pFly->DAY_ITEM_5 >> pFly->DAY_ITEM_6
				>> pFly->DAY_ITEM_DROP_RATE >> pFly->DAY_ITEM_MAX_DROP
				>> pFly->EVENT_BOSS_TID >> pFly->DUNGEON_EXP
				>> pFly->DUNGEON_GOLD;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		clsFlyTree[pFly->DUNGEON] = pFly;
	}


	return true;
}

DungeonData::Flyweight * DungeonData::GetFly(int dungeonid)
{
	auto iter = clsFlyTree.find(dungeonid);

	if( iter == clsFlyTree.end() )
		return NULL;
	else
		return iter->second;
}

} /*NResource*/ } /* namespace ProjectZ */
